/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sp.controller;

import java.awt.CardLayout;
import sp.form.Hal6;
import sp.form.Hal5;
import sp.form.Hal3;
import sp.form.Hal1;
import sp.form.Hal2;
import sp.form.Hal4;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import org.eclipse.persistence.exceptions.DatabaseException;
import sp.component.TextField;
import sp.model.*;
import sp.dao.*;
import sp.form.Hal7;
import sp.util.ErrorControl;
import sp.util.FormControl;
import sp.validasi.*;
import sp.view.TesBeta;

/**
 *
 * @author StephenCN
 */
public class SaveController implements ActionListener {

    private B1Controller b1Controller;
    private B2Controller b2Controller;
    private B3Controller b3Controller;
    private B4Controller b4Controller;
    private B5Controller b5Controller;
    private SisipanController sspController;
    private final Hal1 b1view;
    private final Hal2 b2view;
    private final Hal3 b3view;
    private final Hal4 b4view;
    private final Hal5 b5view;
    private final Hal6 b6view;
    private final Hal7 b7view;
    private B1 b1;
    private B2 b2;
    private B3 b3;
    private B4 b4;
    private B5 b5;
    private Sisipan ssp;
    private final JTable tab;
    private ValidasiB1 validB1;
    private ValidasiB2 validB2;
    private ValidasiB3 validB3;
    private ValidasiB4 validB4;
    private ValidasiB5Sisipan validB5;
    private final JPanel mainPanel;
    private boolean valid;

    public SaveController(B1 b1, B2 b2, B3 b3, B4 b4, B5 b5, Sisipan ssp, Hal1 b1view, Hal2 b2view, Hal3 b3view, Hal4 b4view, Hal5 b5view, Hal6 b6view, Hal7 b7view, JPanel tq, JTable tab) {
        this.b1 = b1;
        this.b2 = b2;
        this.b3 = b3;
        this.b4 = b4;
        this.b5 = b5;
        this.ssp = ssp;
        this.b1view = b1view;
        this.b2view = b2view;
        this.b3view = b3view;
        this.b4view = b4view;
        this.b5view = b5view;
        this.b6view = b6view;
        this.b7view = b7view;
        this.tab = tab;
        this.mainPanel = tq;
    }

    @Override
    public void actionPerformed(ActionEvent ae) {

        b1Controller = new B1Controller(b1, b1view, b7view);
        b2Controller = new B2Controller(b2, b1, b1view);
        b3Controller = new B3Controller(b3, b1, b2view);
        b4Controller = new B4Controller(b4, b1, b3view, b4view, b5view, b6view);
        b5Controller = new B5Controller(b5, b1, b7view);
        sspController = new SisipanController(ssp, b1, b7view);

        b1 = b1Controller.B1hasil();
        b2 = b2Controller.B2hasil();
        b5 = b5Controller.B5hasil();
        if (b1.getB1r9().getKode().equals("1")) {
            b3 = b3Controller.B3hasil();
            b4 = b4Controller.B4hasil();
            ssp = sspController.Sisipanhasil();
        } else {
            b3 = b3Controller.B3nr();
            b4 = b4Controller.B4nr();
            ssp = sspController.Sisipannr();
        }

        validB1 = new ValidasiB1(b1, b1view);
        validB2 = new ValidasiB2(b2);
        if (b1.getB1r9().getKode().equals("1")) {
            validB3 = new ValidasiB3(b3, b2view);
            validB5 = new ValidasiB5Sisipan(b1, b5, ssp, b7view);
        }
        validB4 = new ValidasiB4(b4);

        if (validNotNull()) {
            Validate(b1, b2, b3, b4, b5, ssp);
            if (ErrorControl.getErrList().isEmpty()) {
                b1.setFlag("0");
                try {
                    saveToDB();
                    showSuccesDB();
                    toDefault();
                } catch (Exception ex) {
                    showErrorDB();
                }
            } else {
                String err = "";
                if (!validB1.cek().isEmpty()) {
                    err += "\nB1";
                }
                if (!validB2.cek()) {
                    err += "\nB2";
                }
                if (b1.getB1r9().getKode().equals("1")) {
                    if (!validB3.cek().isEmpty()) {
                        err += "\nB3";
                    }
                }
                if (!validB4.cek()) {
                    err += "\nB4";
                }
                b1.setFlag("1");
                tab.setModel(ErrorControl.getTableRow());
                int ss = JOptionPane.showConfirmDialog(mainPanel,
                        "Apakah Anda Yakin Save Ke DB ?\n" + "Anda Memiliki Error di Blok : \n" + err,
                        "Apakah Anda Yakin Save Ke DB ?",
                        JOptionPane.YES_NO_OPTION);
                if (ss == JOptionPane.YES_OPTION) {
                    try {
                        saveToDB();
                        showSuccesDB();
                        toDefault();
                    } catch (Exception ex) {
                        showErrorDB();
                    }
                }
            }
        } else {
            showNullError();
        }
    }

    public void Validate(B1 b1, B2 b2, B3 b3, B4 b4, B5 b5, Sisipan ssp) {
        ErrorControl.resetErr();
        ValidasiB1 validb1 = new ValidasiB1(b1, b1view);
        if (validb1.cek() != null) {
            ErrorControl.addErr(validb1.cek());
        }
        if (b1.getB1r9().getKode().equals("1")) {
            ValidasiB3 validb3 = new ValidasiB3(b3, b2view);
            ValidasiB5Sisipan validb5 = new ValidasiB5Sisipan(b1, b5, ssp, b7view);
            if (validb3.cek() != null) {
                ErrorControl.addErr(validb3.cek());
            }
            if (validb5.cek() != null) {
                ErrorControl.addErr(validb5.cek());
            }
        }
    }

    public boolean validNotNull() {
        valid = true;

        cekFieldNull(b1view.getListTextField());

        if (b1.getB1r9().getKode().equals("1")) {
            cekFieldNull(b3view.getListTextField());
            cekFieldNull(b4view.getListTextField());
            cekFieldNull(b5view.getListTextField());
            cekFieldNull(b6view.getListTextField());
        }
        return valid;
    }

    private void saveToDB() {
        B1Dao.getInstance().save(b1);
        B2Dao.getInstance().save(b2);
        B3Dao.getInstance().save(b3);
        B4Dao.getInstance().save(b4);
        B5Dao.getInstance().save(b5);
        SisipanDao.getInstance().save(ssp);
    }

    private void showErrorDB() {
        int ss = JOptionPane.showConfirmDialog(mainPanel,
                "Kesalahan Pada Saat Saving Ke DataBase\nSilahkan Cek Kembali Isian Anda",
                "Database Error",
                JOptionPane.WARNING_MESSAGE);
        removeFromDB();
    }

    private void showSuccesDB() {
        String nama;
        if (b3.getB3r1() != null) {
            nama = b3.getB3r1();
        } else {
            nama = "non response";
        }
        int ss = JOptionPane.showConfirmDialog(mainPanel,
                "Sukses Input Dengan \nNKS  =" + b1.getNks() + "\nNAMA =" + b3.getB3r1(),
                "Sukses",
                JOptionPane.CLOSED_OPTION);
    }

    private void showNullError() {
        int ss = JOptionPane.showConfirmDialog(mainPanel,
                "Semua Isian Harus Terisi",
                "Field Kosong",
                JOptionPane.WARNING_MESSAGE);
    }

    private void toDefault() {
        tab.setModel(new TableErrorModel());
        FormControl.clearAll();
        FormControl.getController().first();
        ((CardLayout) mainPanel.getLayout()).previous(mainPanel);
//        TesBeta main = (TesBeta) mainPanel.getParent(); 
//        main.getEntryQuest().getErrDesc().setText("");
    }

    private void cekFieldNull(ArrayList<TextField> arr) {
        for (TextField arr1 : arr) {
            if ("".equals(arr1.getText())) {
                valid = false;
                break;
            }
        }
    }

    private void removeFromDB() {
        try {
            B2 b2del = B2Dao.getInstance().getByID(b1.getNks());
            if (b2del != null) {
                B2Dao.getInstance().delete(b2del);
            }
        } catch (DatabaseException e) {
        }
        try {
            B3 b3del = B3Dao.getInstance().getByID(b1.getNks());
            if (b3del != null) {
                B3Dao.getInstance().delete(b3del);
            }
        } catch (DatabaseException e) {
        }
        try {
            B4 b4del = B4Dao.getInstance().getByID(b1.getNks());
            if (b4del != null) {
                B4Dao.getInstance().delete(b4del);
            }
        } catch (DatabaseException e) {
        }
        try {
            B5 b5del = B5Dao.getInstance().getByID(b1.getNks());
            if (b5del != null) {
                B5Dao.getInstance().delete(b5del);
            }
        } catch (DatabaseException e) {
        }
        try {
            Sisipan sspdel = SisipanDao.getInstance().getByID(b1.getNks());
            if (sspdel != null) {
                SisipanDao.getInstance().delete(sspdel);
            }
        } catch (DatabaseException e) {
        }
        try {
            B1 b1del = B1Dao.getInstance().getByID(b1.getNks());
            if (b1del != null) {
                B1Dao.getInstance().delete(b1del);
            }
        } catch (DatabaseException e) {
        }
    }
}
